package veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
//import java.util.ArrayList;
//import java.util.Collection;
import java.util.Date;
import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Cita;
import veterinaria.modelo.Medicamento;
import veterinaria.util.ConexionBD;


public class CitaDAO extends BaseDAO {

	
	

	public void insertar(Cita vo) throws DAOExcepcion {
         
         String query = "insert into Cita (idCita, fecingreso, idpaciente, iddoctor) values (?,?,?,?)";
         Connection con = null;
         PreparedStatement stmt = null;
         ResultSet rs = null;
         try {
                 con = ConexionBD.obtenerConexion();
                 stmt = con.prepareStatement(query);
                 
                 stmt.setInt(1, vo.getIdCita());
                 stmt.setDate(2, vo.getFecha());
                 stmt.setInt(3, vo.getPaciente());
                 stmt.setInt(4, vo.getDoctor());
                 
                 int i = stmt.executeUpdate();
                 if (i != 1) {
                         throw new SQLException("No se pudo insertar");
                 }
         } catch (SQLException e) {
                 System.err.println(e.getMessage());
                 throw new DAOExcepcion(e.getMessage());
         } finally {
                 this.cerrarResultSet(rs);
                 this.cerrarStatement(stmt);
                 this.cerrarConexion(con);
         }
         //return vo;
 }

	
	public Collection<Cita> buscarPorNombre(String nombre) throws DAOExcepcion {
		String query = "select idcita, fecingreso, idpaciente, iddoctor from Cita where idcita like ?";
		Collection<Cita> lista = new ArrayList<Cita>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cita vo = new Cita();
				vo.setIdCita(rs.getInt("idcita"));
				vo.setFecha(rs.getDate("fecingreso"));
				vo.setPaciente(rs.getInt("idpaciente"));
				vo.setDoctor(rs.getInt("iddoctor"));
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	

	public Cita obtener(int idCita) throws DAOExcepcion {
		Cita vo = new Cita();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idcita, fecingreso, idpaciente, iddoctor from Cita where idcita like ?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idCita);
			rs = stmt.executeQuery();
			if (rs.next()) {
				vo.setIdCita(rs.getInt(1));
				vo.setFecha(rs.getDate(2));
				vo.setPaciente(rs.getInt(3));
				vo.setDoctor(rs.getInt(4));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idCita) throws DAOExcepcion {
		String query = "delete from Cita WHERE idcita=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idCita);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Cita actualizar(Cita vo) throws DAOExcepcion {
		String query = "update cita set fecingreso=?, idpaciente=?,iddoctor=? where idcita=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
		
			stmt.setDate(1, vo.getFecha());
			stmt.setInt(2, vo.getPaciente());
			stmt.setInt(3, vo.getDoctor());
			stmt.setInt(4, vo.getIdCita());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Collection<Cita> listar() throws DAOExcepcion {
		Collection<Cita> c = new ArrayList<Cita>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idCita,fecingreso,idpaciente,iddoctor from cita order by idCita";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cita vo = new Cita();
				vo.setIdCita(rs.getInt("idCita"));
				vo.setFecha(rs.getDate("fecingreso"));
				vo.setPaciente(rs.getInt("idpaciente"));
				vo.setDoctor(rs.getInt("iddoctor"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}
